/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.matezk.dms.things.service.impl;

import cn.matezk.dms.things.mapper.AutoSceneMapper;
import cn.matezk.dms.things.mapper.SceneActionMapper;
import cn.matezk.dms.things.mapper.SceneConditionMapper;
import cn.matezk.dms.things.model.entity.AutoSceneDO;
import cn.matezk.dms.things.model.entity.SceneActionDO;
import cn.matezk.dms.things.model.entity.SceneConditionDO;
import cn.matezk.dms.things.model.query.AutoSceneQuery;
import cn.matezk.dms.things.model.req.AutoSceneReq;
import cn.matezk.dms.things.model.resp.AutoSceneDetailResp;
import cn.matezk.dms.things.model.resp.AutoSceneResp;
import cn.matezk.dms.things.service.AutoSceneService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.continew.starter.extension.crud.service.impl.BaseServiceImpl;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 自动场景业务实现
 *
 * @author 程道林
 * @since 2025/08/28 22:10
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AutoSceneServiceImpl extends BaseServiceImpl<AutoSceneMapper, AutoSceneDO, AutoSceneResp, AutoSceneDetailResp, AutoSceneQuery, AutoSceneReq> implements AutoSceneService {
    private AutoSceneMapper autoSceneMapper;
    private SceneActionMapper sceneActionMapper;
    private SceneConditionMapper sceneConditionMapper;

    @Override
    @Transactional
    public Long add(AutoSceneReq req) {
        Long sceneId = super.add(req);
        AtomicInteger step = new AtomicInteger(1);
        req.getActions().forEach(action -> action.setStep(step.getAndIncrement()));
        sceneActionMapper.insertBatch(req.getActions());
        sceneConditionMapper.insert(req.getConditions());
        return sceneId;
    }

    @Override
    public AutoSceneDetailResp get(Long id) {
        AutoSceneDetailResp resp = super.get(id);
        List<SceneConditionDO> conditions = sceneConditionMapper.selectBySceneId(id);
        resp.setConditions(conditions);
        List<SceneActionDO> actions = sceneActionMapper.selectBySceneId(id);
        resp.setActions(actions);
        return resp;
    }

    @Override
    @Transactional
    public void update(AutoSceneReq req, Long id) {
        super.update(req, id);
        sceneConditionMapper.deleteBySceneId(id);
        sceneConditionMapper.insertBatch(req.getConditions());
        sceneActionMapper.deleteBySceneId(id);
        sceneActionMapper.insertBatch(req.getActions());
    }

    @Override
    public void delete(List<Long> ids) {
        super.delete(ids);
        ids.forEach(id -> {
            sceneConditionMapper.deleteBySceneId(id);
            sceneActionMapper.deleteBySceneId(id);
        });
    }
}